<?php defined('SYSPATH') or die('No direct access allowed.');

class Controller_Admin_Page extends Controller_Admin_Template
{
    public function action_index()
    {       
       
        $this->template->content = View::factory('admin/page/page')
                                       ->bind('pages', $pages)
                                       ->bind('page_links', $page_links);
                                       
      
  	$count = DB::select(DB::expr('COUNT(*) AS `count`'))->from('pages')->execute()->get('count');
  		
  	$pagination = Pagination::factory(array(
  		'total_items'    => $count,
  	));
  		
  	$pages = DB::select()->from('pages')
  			->order_by('last_modified','DESC')
  			->limit($pagination->items_per_page)
  			->offset($pagination->offset)->as_object()->execute();
              
  	$page_links = $pagination->render();
        
        
        
    }
    
    public function action_add()
    {
        if($_POST){
            try
            {
                $page = ORM::factory('page');
                $page->values($_POST);
                $page->created = DB::expr('NOW()');
               
                $page->last_modified = DB::expr('NOW()');
                 
                $page->save();
                
                Request::$initial->redirect('admin/page');
            }
            catch (ORM_Validation_Exception $e)
            {
                $errors = $e->errors('/admin');
            }            
        }
        
        $this->template->styles = array('css/admin/box.css' => 'screen');        
        $this->template->content = View::factory('admin/page/form')
                                       ->bind('errors', $errors)
                                       ->set('data', $_POST)
                                       ->set('title', 'Добавление страницы')
                                       ->set('button', 'Добавить');
    }
    
    public function action_edit()
    {
        $id = Arr::get($_REQUEST, 'id', -1);
        if($id != -1){
            
            
            if($_POST){               
                try
                {
                    $page = ORM::factory('page', $id);
                    $page->values($_POST);
                    $page->last_modified = DB::expr('NOW()');
                    $page->save();
                }
                catch(ORM_Validation_Exception $e)
                {
                    $errors = $e->errors('/admin');
                }                
            }
            
            $page = ORM::factory('page', $id)->as_array();
            
            $this->template->styles = array('css/admin/box.css' => 'screen');
            $this->template->content = View::factory('admin/page/form')
                                           ->bind('errors', $errors)
                                           ->set('data', $page)
                                           ->set('title', 'Редактирование страницы')
                                           ->set('button', 'Сохранить изменения');
                                           
        } else        
            Request::$initial->redirect('admin/page');
    }
    
    public function action_del()
    {
        $id = Arr::get($_REQUEST, 'id', -1);
        if($id != -1){
            ORM::factory('page', $id)->delete();
        }
        Request::$initial->redirect(Request::$initial->referrer());
    }
}